package com.sinodata.bsm.inmp.finder;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.inmp.util.DBConnectionUtil;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-8-14 下午6:11:43          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class GabShujuwangguanFinder extends Finder {

    private static final Logger logger = Logger.getLogger(GabShujuwangguanFinder.class);

    @SuppressWarnings("rawtypes")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String sql = getSql();
        Connection connection = null;
        Statement statement = null;
        ResultSet set = null;
        connection = DBConnectionUtil.getInstance().getStorConnection();
        try {
            statement = connection.createStatement();
            set = statement.executeQuery(sql);
            if (set != null) {
                while (set.next()) {
                    String[] res = new String[7];
                    list.add(res);
                    res[0] = set.getString(1);//内部标示 P_Guid
                    res[1] = set.getString(5) == null || set.getString(5).equals("") ? set.getString(4) : set.getString(4) + "_" + set.getString(5);//资源名称 D_ControlAdderss+"_"+D_BusinessName
                    res[2] = set.getString(2);//资源描述 D_Descr
                    res[3] = set.getString(3);//设备名称 D_HostName
                    res[4] = set.getString(4);//管理地址 D_ControlAdderss
                    res[5] = set.getString(5);//业务名称 D_BusinessName
                    res[6] = set.getString(6);//组件用途 D_Useage
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                DBConnectionUtil.getInstance().returnConn(connection);
            }
        }
        return list;
    }

    private String getSql() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("select ");
        buffer.append("P_Guid, ");
        buffer.append("D_Descr, ");
        buffer.append("D_HostName, ");
        buffer.append("D_ControlAdderss, ");
        buffer.append("D_BusinessName, ");
        buffer.append("D_Useage ");
        buffer.append("from ");
        buffer.append("c_itcomp ");
        buffer.append("where ");
        buffer.append("A_CanManage=1 ");
        buffer.append("and ");
        buffer.append("D_ControlAdderss IS NOT NULL ");
        buffer.append("and ");
        buffer.append("D_HostName IS NOT NULL ");
        String sql = buffer.toString();
        logger.info(">>>>GabShujuwangguanFinder.sql[" + sql + "]");
        return sql;
    }
}
